package org.edu.modules.oa.controller;

import com.alibaba.druid.sql.visitor.functions.If;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.apache.tomcat.jni.FileInfo;
import org.edu.common.api.vo.Result;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oss.OssBootUtil;
import org.edu.modules.oa.entity.VeFileFilesEntity;
import org.edu.modules.oa.entity.VeOaLaneMailEntity;
import org.edu.modules.oa.entity.VeOaMeetEntity;
import org.edu.modules.oa.mapper.IVeFileFilesMapper;
import org.edu.modules.oa.service.IVeOaLaneMailService;
import org.edu.modules.oa.util.OssDownloadUtil;
import org.edu.modules.oa.vo.VeOaCarInfoExportVo;
import org.edu.modules.oa.vo.VeOaMailDetailsVo;
import org.edu.modules.oa.vo.VeOaMailExportVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**写邮件
 * 2021/5/10
 *
 * @author hwj
 */
@Api(tags="内部邮件业务")
@RestController
@RequestMapping("oa/veOaMail")
@Slf4j
public class VeOaMailWriteMailController {
    @Resource
    private IVeOaLaneMailService veOaLaneMailService;
    @Resource
    private IVeFileFilesMapper filesMapper;
    @Resource
    private OSS ossClient;

    @PostMapping("/sendMail")
    public Result<?> sendMail(String toUser,String toUserName,String emailTitle,String emailContext,String fileId){
        System.out.println("测试是否执行");
        //发送邮件
       VeOaLaneMailEntity mail = new VeOaLaneMailEntity();
       mail.setToUser(toUser);
       mail.setToUserName(toUserName);
       mail.setEmailTitle(emailTitle);
       mail.setEmailContent(emailContext);
       mail.setFileId(fileId);
       mail.setEmailRead("发送成功");
        mail.setEmailState("1");
        mail.setIsDeleted(0);
        mail.setTerminalId(1L);
        // 获取redis存的用户值
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        mail.setFromUser(user.getId());
        mail.setFromUserName(user.getUsername());
        System.out.println(mail);
        int num = veOaLaneMailService.sendMail(mail);
        // 收件人信息
        mail.setEmailState("3");
        mail.setEmailRead("未读");
         num = veOaLaneMailService.sendMail(mail);
        if(num == 0){
            return Result.error(500,"发送失败");
        }
        return Result.OK();
    }

    @PostMapping("/uploader")
    @ApiOperation(value="发送邮件", notes="内部邮件--发送邮件")
    public Result<?>  upload(VeOaLaneMailEntity veOaLaneMailEntity)  {
        System.out.println(veOaLaneMailEntity.getFileId());
        // 获取redis存的用户值
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        veOaLaneMailEntity.setEmailRead("发送成功");
        veOaLaneMailEntity.setEmailState("1");
        veOaLaneMailEntity.setIsDeleted(0);
        veOaLaneMailEntity.setTerminalId(1L);
        veOaLaneMailEntity.setFromUser(user.getId());
        veOaLaneMailEntity.setFromUserName(user.getUsername());
        int num2 = veOaLaneMailService.sendMail(veOaLaneMailEntity);

        String[] arr = veOaLaneMailEntity.getToUser().split(",");
        String[] arr1 = veOaLaneMailEntity.getToUserName().split(",");
        veOaLaneMailEntity.setEmailState("3");
        veOaLaneMailEntity.setEmailRead("未读");
        for (int i = 0; i < arr.length; i++) {
            veOaLaneMailEntity.setToUser(arr[i]);
            veOaLaneMailEntity.setToUserName(arr1[i]);
            int num1 = veOaLaneMailService.sendMail(veOaLaneMailEntity);
            if (num1 ==0 ) {
                return Result.error("插入错误，请联系管理员");
            }
        }
        return Result.OK();
    }
    /**
     * 从草稿箱发送信息流
     * param file 文件信息流
     * @param veOaLaneMailEntity 邮件类
     * @return
     */
    @PostMapping("/sendDraft")
    @ApiOperation(value="发送邮件从草稿箱", notes="内部邮件--发送邮件从草稿箱")
    public Result<?>  sendDraft(VeOaLaneMailEntity veOaLaneMailEntity)  {
        // 获取redis存的用户值
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        veOaLaneMailEntity.setEmailRead("发送成功");
        veOaLaneMailEntity.setEmailState("1");
        veOaLaneMailEntity.setIsDeleted(0);
        veOaLaneMailEntity.setTerminalId(1L);
        veOaLaneMailEntity.setFromUser(user.getId());
        veOaLaneMailEntity.setFromUserName(user.getUsername());
        //获取当前时间
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowDate = f.format(date);
        veOaLaneMailEntity.setCreateTime(date);
        System.out.println(veOaLaneMailEntity);
        Boolean num2 = veOaLaneMailService.updateById(veOaLaneMailEntity);
        System.out.println("打印更新草稿结果"+num2);
        String[] arr = veOaLaneMailEntity.getToUser().split(",");
        String[] arr1 = veOaLaneMailEntity.getToUserName().split(",");
        veOaLaneMailEntity.setEmailState("3");
        veOaLaneMailEntity.setEmailRead("未读");
        for (int i = 0; i < arr.length; i++) {
            veOaLaneMailEntity.setToUser(arr[i]);
            veOaLaneMailEntity.setToUserName(arr1[i]);
            int num1 = veOaLaneMailService.sendMail(veOaLaneMailEntity);
            if (num1 ==0 ) {
                return Result.error("插入错误，请联系管理员");
            }
        }
        return Result.OK();
    }
    /**
     * 修改草稿箱
     * param file 文件信息流
     * @param veOaLaneMailEntity 邮件类
     * @return
     */
    @PostMapping("/saveDraft")
    @ApiOperation(value="修改草稿", notes="内部邮件--修改草稿")
    public Result<?> saveDraft(VeOaLaneMailEntity veOaLaneMailEntity)  {
        //获取当前时间
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowDate = f.format(date);
        veOaLaneMailEntity.setCreateTime(date);
        System.out.println(veOaLaneMailEntity);
        // 获取redis存的用户值
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        System.out.println(veOaLaneMailEntity);
        Boolean num2 = veOaLaneMailService.updateById(veOaLaneMailEntity);
        System.out.println("打印更新草稿结果"+num2);
        return Result.OK();
    }


    /**
     * 草稿箱
     * @param
     * @return 返回类
     */
    @PostMapping("/moveMailToDraft")
    @ApiOperation(value="保存草稿", notes="内部邮件--保存草稿")
    public Result<?> moveMailToDraft(VeOaLaneMailEntity veOaLaneMailEntity){
        System.out.println(veOaLaneMailEntity);
        // 获取redis存的用户值
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        veOaLaneMailEntity.setEmailRead("草稿");
        veOaLaneMailEntity.setEmailState("2");
        veOaLaneMailEntity.setIsDeleted(0);
        veOaLaneMailEntity.setTerminalId(1L);
        veOaLaneMailEntity.setFromUser(user.getId());
        veOaLaneMailEntity.setFromUserName(user.getUsername());
        int num1 = veOaLaneMailService.sendMail(veOaLaneMailEntity);
        return Result.OK();
    }
    /**
     * 收件箱-动态分页查询
     * @param titleQuery 查询主题
     * @return 查询结果
     */
    @PostMapping("/receiveMail")
    @ApiOperation(value="收件箱查询", notes="内部邮件--收件箱查询")
    public Result<?> receiveMail(String titleQuery,Integer startPage, Integer pageSize){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<VeOaLaneMailEntity> listTotal = veOaLaneMailService.list(
                new QueryWrapper<VeOaLaneMailEntity>()
                        .eq("is_deteted",0)
                        .eq("emailstate",3)
                        .eq("toUser",user.getId())
                        .like("emailTitle",titleQuery).orderByDesc("create_time"));
        IPage<VeOaLaneMailEntity> list  =  veOaLaneMailService.page(new Page<VeOaLaneMailEntity>(startPage,pageSize),
                new QueryWrapper<VeOaLaneMailEntity>()
                        .eq("is_deteted",0)
                        .eq("emailstate",3)
                        .eq("toUser",user.getId())
                        .like("emailTitle",titleQuery).orderByDesc("create_time"));
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("listTotal",listTotal);
        return Result.OK(map);
    }
    /**
     * 已发送-动态分页查询
     * @param titleQuery 查询主题
     * @return 查询结果
     */
    @PostMapping("/hasMailQuery")
    @ApiOperation(value="已发送查询", notes="内部邮件--已发送查询")
    public Result<?> hasMailQuery(String titleQuery,Integer startPage, Integer pageSize){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<VeOaLaneMailEntity> listTotal = veOaLaneMailService.list(
                new QueryWrapper<VeOaLaneMailEntity>()
                        .eq("is_deteted",0)
                        .eq("emailstate",1)
                        .eq("fromUser",user.getId())
                        .like("emailTitle",titleQuery).orderByDesc("create_time"));
        IPage<VeOaLaneMailEntity> list  =  veOaLaneMailService.page(new Page<VeOaLaneMailEntity>(startPage,pageSize),
                new QueryWrapper<VeOaLaneMailEntity>()
                .eq("is_deteted",0)
                .eq("emailstate",1)
                .eq("fromUser",user.getId())
                .like("emailTitle",titleQuery).orderByDesc("create_time"));
        System.out.println(list);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("listTotal",listTotal);
        return Result.OK(map);
    }
    /**
     * 草稿箱-动态分页查询
     * @param titleQuery 查询主题
     * @return 查询结果
     */
    @PostMapping("/draftsQuery")
    @ApiOperation(value="草稿箱查询", notes="内部邮件--草稿箱查询")
    public Result<?> draftsQuery(String titleQuery,Integer startPage, Integer pageSize){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<VeOaLaneMailEntity> listTotal = veOaLaneMailService.list(
                new QueryWrapper<VeOaLaneMailEntity>()
                        .eq("is_deteted",0)
                        .eq("emailstate",2)
                        .eq("fromUser",user.getId())
                        .like("emailTitle",titleQuery).orderByDesc("create_time"));
        IPage<VeOaLaneMailEntity> list  =  veOaLaneMailService.page(new Page<VeOaLaneMailEntity>(startPage,pageSize),
                new QueryWrapper<VeOaLaneMailEntity>()
                        .eq("is_deteted",0)
                        .eq("emailstate",2)
                        .eq("fromUser",user.getId())
                        .like("emailTitle",titleQuery).orderByDesc("create_time"));
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("listTotal",listTotal);
        return Result.OK(map);
    }
    /**
     * 已删除-动态分页查询
     * @param titleQuery 查询主题
     * @return 查询结果
     */
    @PostMapping("/removeMailQuery")
    @ApiOperation(value="已删除查询", notes="内部邮件--已删除查询")
    public Result<?> removeMailQuery(String titleQuery,Integer startPage, Integer pageSize){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<VeOaLaneMailEntity> listTotal = veOaLaneMailService.list(
                new QueryWrapper<VeOaLaneMailEntity>()
                        .and(z -> z.and(i -> i.eq("fromUser",user.getId()).eq("emailstate","1"))
                                .or(j -> j.eq("toUser",user.getId()).eq("emailstate","3")))
                        .eq("is_deteted",1)
                        .like("emailTitle",titleQuery).orderByDesc("create_time"));
        IPage<VeOaLaneMailEntity> list  =  veOaLaneMailService.page(new Page<VeOaLaneMailEntity>(startPage,pageSize),
                new QueryWrapper<VeOaLaneMailEntity>()
                        .and(z -> z.and(i -> i.eq("fromUser",user.getId()).eq("emailstate","1"))
                                .or(j -> j.eq("toUser",user.getId()).eq("emailstate","3")))
                        .eq("is_deteted",1)
                        .like("emailTitle",titleQuery).orderByDesc("create_time"));
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("listTotal",listTotal);
        return Result.OK(map);
    }
    @PostMapping("/showDetails")
    @ApiOperation(value="展示详情", notes="内部邮件--展示详情")
    public Result<?> showDetails(Long id){
        VeOaLaneMailEntity mail = veOaLaneMailService.getById(id);
        VeFileFilesEntity file = filesMapper.selectById(mail.getFileId());
        VeOaMailDetailsVo mailDetailsVo = new VeOaMailDetailsVo();
        mailDetailsVo.setMail(mail);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //判断查看者是收件人,如果是修改阅读状态
        if(user.getId().equals(mail.getToUser())){
            VeOaLaneMailEntity updateMail = new VeOaLaneMailEntity();
            updateMail.setEmailRead("已读");
            updateMail.setId(id);
            boolean num = veOaLaneMailService.updateById(updateMail);
            System.out.println("更新阅读状态"+num);
        }
        if (file!=null){
            mailDetailsVo.setFileName(file.getName());
            mailDetailsVo.setSourceFile(file.getSourceFile());
        }
        return Result.OK(mailDetailsVo);
    }

    /**
     * 转发
     * @param mail 文件类型
     * @return 返回
     */
    @PostMapping("/transpond")
    @ApiOperation(value="转发邮件", notes="内部邮件--转发邮件")
    public Result<?> transpondMail(VeOaLaneMailEntity mail){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        mail.setEmailRead("发送成功");
        mail.setEmailState("1");
        mail.setIsDeleted(0);
        mail.setTerminalId(1L);
        mail.setFromUser(user.getId());
        mail.setFromUserName(user.getUsername());
        int num = veOaLaneMailService.sendMail(mail);
        String[] arr = mail.getToUser().split(",");
        String[] arr1 = mail.getToUserName().split(",");
        mail.setEmailState("3");
        mail.setEmailRead("未读");
        for (int i = 0; i < arr.length; i++) {
            mail.setToUser(arr[i]);
            mail.setToUserName(arr1[i]);
            int num1 = veOaLaneMailService.sendMail(mail);
            if (num1 ==0 ) {
                return Result.error("插入错误，请联系管理员");
            }
        }
        return Result.OK();
    }
    @PostMapping("/removeMail")
    @ApiOperation(value="删除邮件至已删除", notes="内部邮件--删除邮件至已删除")
    public Result<?> removeMail(String idStr){
        String[] idArr = idStr.split(",");
        for (String s : idArr) {
            System.out.println(s);
            VeOaLaneMailEntity mail = new VeOaLaneMailEntity();
            mail.setId(Long.valueOf(s));
            mail.setIsDeleted(1);
            boolean num = veOaLaneMailService.updateById(mail);
        }
        return Result.OK();
    }
    @PostMapping("/sendMailFromDraft")
    @ApiOperation(value="多条草稿发送", notes="内部邮件--多条草稿发送")
    public Result<?> sendMailFromDraft(String idStr){
        String[] idArr = idStr.split(",");
        for (String s : idArr) {
            System.out.println(s);
            VeOaLaneMailEntity mail = veOaLaneMailService.getById(s);
            System.out.println(mail);
            if(mail.getToUser()==null || "".equals(mail.getToUser())){
                return Result.error("邮件发送中止，请输入发送对象");
            }
            if (mail.getEmailContent()==null || "".equals(mail.getEmailContent())){
                return Result.error("邮件发送中止。内容不能为空,请检查后重试");
            }
            if(mail.getEmailTitle() ==null || "".equals(mail.getEmailContent()) ){
                return Result.error("邮件发送中止。主题不能为空,请输入完整后重试");
            }
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            mail.setEmailRead("发送成功");
            mail.setEmailState("1");
            mail.setIsDeleted(0);
            mail.setTerminalId(1L);
            mail.setFromUser(user.getId());
            mail.setFromUserName(user.getUsername());
            //获取当前时间
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String nowDate = f.format(date);
            mail.setCreateTime(date);
            System.out.println(mail);
            Boolean num2 = veOaLaneMailService.updateById(mail);
            System.out.println("打印更新草稿结果"+num2);
            String[] arr = mail.getToUser().split(",");
            String[] arr1 = mail.getToUserName().split(",");
            mail.setEmailState("3");
            mail.setEmailRead("未读");
            for (int i = 0; i < arr.length; i++) {
                mail.setToUser(arr[i]);
                mail.setToUserName(arr1[i]);
                int num1 = veOaLaneMailService.sendMail(mail);
                if (num1 ==0 ) {
                    return Result.error("插入错误，请联系管理员");
                }
            }
        }
        return Result.OK();
    }
    @PostMapping("/deleteMail")
    @ApiOperation(value="逻辑删除", notes="内部邮件--逻辑删除")
    public Result<?> deleteMail(String idStr){
        String[] idArr = idStr.split(",");
        Collection<VeOaLaneMailEntity> list = new ArrayList<VeOaLaneMailEntity>();
        for (String s : idArr) {
            VeOaLaneMailEntity mail = new VeOaLaneMailEntity();
            mail.setId(Long.valueOf(s));
            mail.setIsDeleted(2);
            list.add(mail);
        }
        boolean num = veOaLaneMailService.updateBatchById(list,list.size());
        if (num){
            return Result.OK();
        }
        return Result.error("错误终止");
    }
    @PostMapping("/recoverMail")
    @ApiOperation(value="恢复邮件", notes="内部邮件--恢复邮件")
    public Result<?> recoverMail(String idStr){
        String[] idArr = idStr.split(",");
        Collection<VeOaLaneMailEntity> list = new ArrayList<VeOaLaneMailEntity>();
        for (String s : idArr) {
            VeOaLaneMailEntity mail = new VeOaLaneMailEntity();
            mail.setId(Long.valueOf(s));
            mail.setIsDeleted(0);
            list.add(mail);
        }
        boolean num = veOaLaneMailService.updateBatchById(list,list.size());
        if (num){
            return Result.OK();
        }
        return Result.error("错误终止");
    }

    @GetMapping(value = "/exportRemoveMail")
    @ApiOperation(value="导出已删除", notes="内部邮件--导出已删除")
    /**
     * 导出表格流：已删除
     */
    public ModelAndView exportTable() {
        //处理条件
        VeOaLaneMailEntity mail = new VeOaLaneMailEntity();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        mail.setFromUser(user.getId());
        mail.setIsDeleted(1);
        // 创建导出流
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        // 查数据获得加油表格的集合
        List<VeOaMailExportVo> list = veOaLaneMailService.selectAllByIsDeletedAndToUserAndEmailState(mail);
        // 文件的文件名
        mv.addObject(NormalExcelConstants.FILE_NAME, "已删除");
        // 数据的类型
        mv.addObject(NormalExcelConstants.CLASS, VeOaMailExportVo.class);
        // 获取redis存的用户值

        // ExportParams  Export工具类   （参数1--》title:表格标题  参数2--》secomdTitle:第二行的标题，  参数3--》secomdTitle:最下面尾部的信息）
        ExportParams exportParams = new ExportParams("已删除邮件列表", "导出人:" + user.getRealname(), "导出邮件信息");
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, list);
        return mv;
    }
    @GetMapping(value = "/exportReceiveMail")
    @ApiOperation(value="导出收件箱", notes="内部邮件--导出收件箱")
    /**
     * 导出表格流：收件箱
     */
    public ModelAndView exportReceiveMail() {
        //处理条件
        VeOaLaneMailEntity mail = new VeOaLaneMailEntity();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        mail.setToUser(user.getId());
        mail.setEmailState("3");
        mail.setIsDeleted(0);
        // 创建导出流
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        // 查数据获得加油表格的集合
        List<VeOaMailExportVo> list = veOaLaneMailService.selectAllByIsDeletedAndToUserAndEmailState(mail);
        // 文件的文件名
        mv.addObject(NormalExcelConstants.FILE_NAME, "收件箱");
        // 数据的类型
        mv.addObject(NormalExcelConstants.CLASS, VeOaMailExportVo.class);

        // ExportParams  Export工具类   （参数1--》title:表格标题  参数2--》secomdTitle:第二行的标题，  参数3--》secomdTitle:最下面尾部的信息）
        ExportParams exportParams = new ExportParams("收件箱邮件列表", "导出人:" + user.getRealname(), "导出邮件信息");
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, list);
        return mv;
    }
    @GetMapping(value = "/exportDraftMail")
    @ApiOperation(value="导出草稿箱", notes="内部邮件--导出草稿箱")
    /**
     * 导出表格流：草稿箱
     */
    public ModelAndView exportDraftMail() {
        //处理条件
        VeOaLaneMailEntity mail = new VeOaLaneMailEntity();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        mail.setFromUser(user.getId());
        mail.setEmailState("2");
        mail.setIsDeleted(0);
        // 创建导出流
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        // 查数据获得加油表格的集合
        List<VeOaMailExportVo> list = veOaLaneMailService.selectAllByIsDeletedAndToUserAndEmailState(mail);
        // 文件的文件名
        mv.addObject(NormalExcelConstants.FILE_NAME, "草稿箱");
        // 数据的类型
        mv.addObject(NormalExcelConstants.CLASS, VeOaMailExportVo.class);
        // 获取redis存的用户值

        // ExportParams  Export工具类   （参数1--》title:表格标题  参数2--》secomdTitle:第二行的标题，  参数3--》secomdTitle:最下面尾部的信息）
        ExportParams exportParams = new ExportParams("草稿箱邮件列表", "导出人:" + user.getRealname(), "导出邮件信息");
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, list);
        return mv;
    }
    @GetMapping(value = "/exportHasMail")
    @ApiOperation(value="导出已发送", notes="内部邮件--导出已发送")
    /**
     * 导出表格流：已发送
     */
    public ModelAndView exportHasMail() {
        //处理条件
        VeOaLaneMailEntity mail = new VeOaLaneMailEntity();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        mail.setFromUser(user.getId());
        mail.setEmailState("3");
        mail.setIsDeleted(0);
        // 创建导出流
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        // 查数据获得加油表格的集合
        List<VeOaMailExportVo> list = veOaLaneMailService.selectAllByIsDeletedAndToUserAndEmailState(mail);
        // 文件的文件名
        mv.addObject(NormalExcelConstants.FILE_NAME, "已发送");
        // 数据的类型
        mv.addObject(NormalExcelConstants.CLASS, VeOaMailExportVo.class);
        // 获取redis存的用户值

        // ExportParams  Export工具类   （参数1--》title:表格标题  参数2--》secomdTitle:第二行的标题，  参数3--》secomdTitle:最下面尾部的信息）
        ExportParams exportParams = new ExportParams("已发送邮件列表", "导出人:" + user.getRealname(), "导出邮件信息");
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, list);
        return mv;
    }


    @PostMapping("/sendFile")
    @ApiOperation(value="测试上传文件", notes="内部邮件--测试上传文件")
    public Result<?>  send(MultipartFile file)  throws Exception{
        //创建文件类
        VeFileFilesEntity veFileFilesEntity = null;
        String fileName = null;
        String suffixName = null;
        if(file != null) {
            //把MultipartFile文件流转换成文件
            File document = null;
            //获取文件名
            fileName = file.getOriginalFilename();
            //用点来切割获取文件类型注意//.需
            //要加双斜杠正则表达式
            String[] arr = fileName.split("\\.");
            //获取文件后缀名
            suffixName = arr[arr.length - 1];
            System.out.println("打印后缀名"+suffixName);
            if ("java".equals(suffixName) || "class".equals(suffixName)) {
                return Result.error(400, "文件类型不能上传");
            }
            String[] filename = null;
            try {
                String originalFilename = file.getOriginalFilename();
                filename = originalFilename.split("\\.");
                if (filename[0].length()>3){
                    document = File.createTempFile(filename[0], filename[1]);
                }else {
                    document = File.createTempFile(filename[0] + "123", filename[1]);
                }
                file.transferTo(document);
                document.deleteOnExit();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //获取文件长度
            Long size = file.getSize();
            //获取唯一的文件名称
            String docName = UUID.randomUUID().toString();
            //上传文件到指定的存储空间  第一个参数 bucketName  第二个参数 OSS存储路径  第三个参数 要上传的文件
            PutObjectResult result = ossClient.putObject("exaplebucket-beijing", docName+"/"+fileName, document);
            //文件下载地址
            String path = "https://exaplebucket-beijing.oss-cn-beijing.aliyuncs.com/" + fileName;
            veFileFilesEntity = new VeFileFilesEntity();
            //当前插入的时间
            Long time = System.currentTimeMillis() / 1000;
            veFileFilesEntity.setSourceFile("https://exaplebucket-beijing.oss-cn-beijing.aliyuncs.com/"+docName+"/"+filename);
            veFileFilesEntity.setName(fileName);
            veFileFilesEntity.setExt(suffixName);
            veFileFilesEntity.setCreateTime(time);
            veFileFilesEntity.setSize(size);
            int num = filesMapper.insert(veFileFilesEntity);
            // 获取redis存的用户值
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            return Result.OK(veFileFilesEntity);
        }else {
            return Result.error("文件为空");
        }

    }
}
